虚拟机ubuntu下caffe与faster-rcnn的only CPU安装教程.尝试记录安装过程,给以后配置提供参考
主要参考1http://blog.csdn.net/zyb19931130/article/details/53842791
主要参考2http://blog.csdn.net/zoro_lov3/article/details/60581174
提前认识
硬件环境
- RAM :8G
- CPU: i5-3230M CPU @ 2.60GHz
- OS:Windows8.1
- GPU:GT 740M
软件环境(VM虚拟机中)
- OS:Ubuntu 16.04
- 安装opencv2.4.10
建议给Ubuntu分配不低于3G的内存
一般依赖项安装
1 | sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler |
Opencv2.4.10安装
文件下载
1 | git clone https://github.com/jayrambhia/Install-OpenCV |
安装Opencv依赖项
文件dependencies.sh在Ubuntu目录1
2cd Ubuntu
sudo ./dependencies.sh
安装Opencv2.4.10
1 | cd 2.4 |
Opencv安装比较久,安装完成之后会显示OpenCV 2.4.10 ready to be used
Opencv测试
- 切换到opencv下载解压后的文件夹目录下,然后进入sample/c/目录下,编译样例文件,具体如下:
cd ~/下载/opencv-2.4.10/samples/c/
如下载在Ubuntu/2.4/Opencv中,则输入
1 |
|
执行完成后,会生成对应的可执行文件
- 运行其中一个样例,如
1
./find_ob
显示图像则为正确安装
caffe安装
下载caffe
1 | cd ~ |
复制配置文件
1 | cd caffe |
修改配置文件
1 | gedit Makefile.config |
ctrl +F
找到以下几行,并做如下修改1
2
3
4
5
6
7
8
9
10
11
12
13去掉注释CPU_ONLY :=1
注释掉CUDA有关的行:
#CUDA_DIR := /usr/local/cuda
#CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
# -gencode arch=compute_20,code=sm_21 \
# -gencode arch=compute_30,code=sm_30 \
# -gencode arch=compute_35,code=sm_35 \
# -gencode arch=compute_50,code=sm_50 \
# -gencode arch=compute_50,code=compute_50
去掉注释WITH_PYTHON_LAYER := 1
这一行有所改动INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
这一行有所改动LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/i386-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
注释掉这一行#TEST_GPUID := 0
编译文件
1 | make all |
若在编译过程中,没有报错 ,则进入下一步
尝试import caffe
1 | $cd caffe/python |
如果没有报错,需要继续输入1
dir(caffe)
如果显示1
2
3
4
5
6['AdaDeltaSolver', 'AdaGradSolver', 'AdamSolver', 'Classifier', 'Detector', 'Layer',
'NesterovSolver', 'Net', 'NetSpec', 'RMSPropSolver', 'SGDSolver', 'TEST', 'TRAIN',
'__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__',
'__version__', '_caffe', 'classifier', 'detector', 'get_solver', 'io', 'layer_type_list',
'layers', 'net_spec', 'params', 'proto', 'pycaffe', 'set_device', 'set_mode_cpu', 'set_mode_gpu',
'set_random_seed', 'to_proto']
说明安装成功。如果报错,一般是部分依赖项没有安装上,请参考最开始的一般依赖项检查。
py-faster-rcnn的CPU安装
py-faster-rcnn下载
1 | cd ~ |
安装安装cython和easydict
1 | sudo pip install cython |
编译cython并做如下修改
1 | cd py-faster-rcnn/lib |
crtl+f
查找,将setup.py文件中部分行 ,修改为如下所示1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#CUDA = locate_cuda()
#self.set_executable('compiler_so', CUDA['nvcc'])
#Extension('nms.gpu_nms',
#['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
#library_dirs=[CUDA['lib64']],
#libraries=['cudart'],
#language='c++',
#runtime_library_dirs=[CUDA['lib64']],
# this syntax is specific to this build system
# we're only going to use certain compiler args with nvcc and not with
# gcc the implementation of this trick is in customize_compiler() below
#extra_compile_args={'gcc': ["-Wno-unused-function"],
# 'nvcc': ['-arch=sm_35',
# '--ptxas-options=-v',
# '-c',
# '--compiler-options',
# "'-fPIC'"]},
#include_dirs = [numpy_include, CUDA['include']]
#),
编译 文件1
make
修改并编译caffe
直接到之前的caffe目录,找到Makefile.config,然后复制粘贴到/py-faster-rcnn/caffe-fast-rcnn/
打开CMakeLists.txt,做如下修改(OFF改成ON)
1
caffe_option(CPU_ONLY "Build Caffe without CUDA support" ON) # TODO: rename to USE_CUDA
编译cafe
1
2cd ~/py-faster-rcnn/caffe-fast-rcnn
make -j8&& make pycaffe
下载测试文件
采用ubuntu下载
1
2cd ~/py-faster-rcnn
./data/scripts/fetch_faster_rcnn_models.sh外部下载工具下载,然后移动到py-faster-rcnn/data中
下载链接https://dl.dropboxusercontent.com/s/o6ii098bu51d139/faster_rcnn_models.tgz?dl=0
下载完成之后 ,解压1
tar zxvf faster_rcnn_models.tgz -C ./
修改部分文件
修改/py-faster-rcnn/lib/fast_rcnn/config.py文件(True改成False)
1
2# Use GPU implementation of non-maximum suppression
__C.USE_GPU_NMS = False将/py-faster-rcnn/tools/test_net.py和 /py-faster-rcnn/tools/train_net.py的caffe.set_mode_gpu()修改为caffe.set_mode_cpu().
修改/py-faster-rcnn/lib/fast_rcnn/nms_wrapper.py文件(注释该引用,并将False改成True)
1
2#from nms.gpu_nms import gpu_nms
def nms(dets, thresh, force_cpu=True):
运行demo
1 | cd ~/py-faster-rcnn |
如果显示1
2
3syncedmem.hpp:25] Check failed: *ptr host allocation of size 345600000 failed
*** Check failure stack trace: ***
已放弃 (核心已转储)
个人猜测是内存不足的原因,如有其他解决方案,烦请评论提醒。此处尝试更换测试数据集,如下1
2cd ~/py-faster-rcnn
sudo ./tools/demo.py --cpu --net zf
稍等片刻既可
最后清理系统垃圾
在安装过程中,可能产生系统垃圾或者不需要的软件,可以将其清理掉
点这里参考这篇博客解决